package com.myt.matp.utils;
   
import javax.microedition.lcdui.Canvas;  

public final class Platform {

    public static final int STANDARD_ID = 0;  
    public static final int NOKIA_ID = 1;  
    public static final int MOTOROLA_ID = 2;
    public static final int SONYERICSSON_ID = 3;  
    public static final int SIEMENS_ID = 4;  
    public static final int SAMSUNG_ID = 5;  
    public static final int LG_ID = 6;  

    private static final String NAME[] = { 
	"standard",
	"nokia", 
	"motorola",
	"SonyEricsson",
	"siemens",
	"samsung",
	"LG" 
    };

    private static final String SOFT_WORD = "SOFT";  

    public static int id() {
	int ret = STANDARD_ID;
	if (isNokia()) {
	    ret = NOKIA_ID;
	} else if (isSonyEricsson()) {
	    ret = SONYERICSSON_ID;
	} else if (isSamsung()) {
	    ret = SAMSUNG_ID;
	} else if (isMotorola()) {
	    ret = MOTOROLA_ID;
	} else if (isLG()) {
	    ret = LG_ID;
	} else if (isSiemens()) {
	    ret = SIEMENS_ID;
	}
	return ret;
    }

    public static int id(Canvas c) {
	int ret = STANDARD_ID;
	if (isMotorola(c)) {
	    ret = MOTOROLA_ID;
	} else {
	    ret = id();
	}
	return ret;
    }

    public static String name() {
	return getName(id());
    }

    public static String name(Canvas c) {
	return (getName(id(c)));
    }

    public static String getName(int id) {
	return NAME[id];
    }

    public static boolean isNokia() {
	return detectByProperty(getName(NOKIA_ID));
    }

    public static boolean isSonyEricsson() {
	return detectByProperty(getName(SONYERICSSON_ID));
    }

    public static boolean isSamsung() {
	return (detectByProperty(getName(SAMSUNG_ID)) ||
		detectByClassName("com.samsung.util.Vibration"));
    }

    public static boolean isMotorola() {
	return (detectByProperty(getName(MOTOROLA_ID)) ||
		detectByClassName("com.motorola.graphics.j3d.Effect3D") ||
		detectByClassName("com.motorola.multimedia.Vibrator") ||
		detectByClassName("com.motorola.multimedia.Lighting") ||
		detectByClassName("com.motorola.multimedia.FunLight"));
    }

    public static boolean isMotorola(Canvas c) {
	return (isMotorola() ||
		detectByCanvasKeyCode(c, -21) || // Left soft key
		detectByCanvasKeyCode(c, 21) ||	 // Left soft key
		detectByCanvasKeyCode(c, -20) || // Left soft key
		detectByCanvasKeyCode(c, -22) || // Right soft key
		detectByCanvasKeyCode(c, 22) || // Right soft key
		detectByCanvasKeyCode(c, -23)	// middle soft key
		);
    }

    public static boolean isSiemens() {
	return (detectByProperty(getName(SIEMENS_ID)) ||
		detectByClassName("com.siemens.mp.io.File"));
    }

    public static boolean isLG() {
	return (detectByProperty(getName(LG_ID)) ||
		detectByClassName("mmpp.media.MediaPlayer") ||
		detectByClassName("mmpp.phone.Phone") ||
		detectByClassName("mmpp.lang.MathFP") ||
		detectByClassName("mmpp.media.BackLight"));
    }

    public static boolean detectByProperty(final String platform) {
	boolean ret = false;
	try {  
	    String currentPlatform = System.getProperty("microedition.platform");  
	    ret = (currentPlatform.toUpperCase().indexOf(platform.toUpperCase()) != -1);
	    currentPlatform = null;
	} catch (Exception ex) {  
	    ret = false;
	}
	finally {
	    System.gc();
	}
	return ret;
    }

    public static boolean detectByClassName(final String classname) {
	boolean ret = false;
	try {  
	    Class t = Class.forName(classname);
	    ret = (t != null);
	    t = null;
	} catch (Exception ex) {  
	    ret = false;
	}
	finally {
	    System.gc();
	}
	return ret;
    }

    public static boolean detectByCanvasKeyCode(final Canvas c, int softkeycode) {
	boolean ret = false;
	try {  
	    if (c.getKeyName(softkeycode).toUpperCase().indexOf(SOFT_WORD) > -1) {  
		ret = true;
	    }  
	} catch (Exception e) {
	    ret = false;
	}
	return ret;
    }

}